﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.DAL.DAOs;
using System.Web.Security;

namespace LibrarySystem.Controllers.LibraryControllers
{
    public class BorrowingController : Controller
    {
        
        public ActionResult LibraryBorrow()
        {
            Library_Employees libEmp = new Library_Employees();
            var empFilter = libEmp.getEmpToComboBox().Select(c => new SelectListItem { Text = c.UserName, Value = c.ID.ToString() }).ToList();
            empFilter.Insert(0, new SelectListItem { Selected = true, Value = "", Text = "---Nhân viên---" });
            ViewBag.Employees = empFilter;

            return View();
        }

        [HttpPost]
        public JsonResult GetBorrowingList(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null,
            string studentCode = null, int lenderId = 0, int isReturned = -1)
        {
            Library_Borrowings libBor = new Library_Borrowings();
            try
            {
                int count = libBor.GetDataCount(studentCode, lenderId, isReturned);
                List<BorrowingModel> borrowList = libBor.GetDataList(jtStartIndex, jtPageSize, jtSorting, studentCode, lenderId, isReturned);
                return Json(new { Result = "OK", Records = borrowList, TotalRecordCount = count });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult AddBorrowing(BorrowingModel model)
        {
            Library_Borrowings libBor = new Library_Borrowings();
            try
            {

                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
                //
                int employeeID = libBor.GetEmployeeIdByUsername(currentUser.UserName);
                model.LenderID = employeeID;
                var add = libBor.AddNewData(model);
                return Json(new { Result = "OK", Record = add });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult UpdateBorrowing(BorrowingModel model)
        {
            Library_Borrowings libBor = new Library_Borrowings();
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libBor.UpdateData(model);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult DeleteBorrowing(int Id)
        {
            Library_Borrowings libBor = new Library_Borrowings();
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libBor.DeleteData(Id);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

    }
}
